package cn.dansj.common.utils.json;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

class JSONMapper extends ObjectMapper {
    public JSONMapper() {
        super();
    }

    public JSONMapper(ObjectMapper src) {
        super(src);
    }

    public JSONMapper(ObjectMapper src, JsonFactory jsonFactory) {
        super(src, jsonFactory);
    }

    public <T> T readValue(String content, Class<T> valueType) throws JsonProcessingException {
        return super.readValue(CommonUtils.addQuoted(CommonUtils.cleanLongSuffixes(content)), valueType);
    }

    public <T> T readValue(String content, TypeReference<T> valueTypeRef) throws JsonProcessingException {
        return super.readValue(CommonUtils.addQuoted(CommonUtils.cleanLongSuffixes(content)), valueTypeRef);
    }

    public <T> T readValue(String content, JavaType valueType) throws JsonProcessingException {
        return super.readValue(CommonUtils.addQuoted(CommonUtils.cleanLongSuffixes(content)), valueType);
    }

    public JsonNode readTree(String content) throws JsonProcessingException {
        return super.readTree(CommonUtils.addQuoted(CommonUtils.cleanLongSuffixes(content)));
    }

    @Override
    public JSONMapper copy() {
        _checkInvalidCopy(JSONMapper.class);
        return new JSONMapper(this);
    }

    @Override
    public JSONMapper copyWith(JsonFactory factory) {
        _checkInvalidCopy(JSONMapper.class);
        return new JSONMapper(this, factory);
    }

    @SuppressWarnings("deprecation")
    public JSONMapper configure(MapperFeature f, boolean state) {
        _serializationConfig = state ? _serializationConfig.with(f) : _serializationConfig.without(f);
        _deserializationConfig = state ? _deserializationConfig.with(f) : _deserializationConfig.without(f);
        return this;
    }

    @SuppressWarnings("deprecation")
    public JSONMapper enable(MapperFeature... f) {
        _deserializationConfig = _deserializationConfig.with(f);
        _serializationConfig = _serializationConfig.with(f);
        return this;
    }

    @SuppressWarnings("deprecation")
    public JSONMapper disable(MapperFeature... f) {
        _deserializationConfig = _deserializationConfig.without(f);
        _serializationConfig = _serializationConfig.without(f);
        return this;
    }
}
